package com.cslc.wechat.bind.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;




import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.cslc.wechat.bind.service.BindService;
import com.cslc.wechat.common.action.BaseAction;
import com.cslc.wechat.common.bean.Clerk;
import com.cslc.wechat.common.bean.Terminal;

public class BindAction extends BaseAction{
	private static Logger logger = Logger.getLogger(BindAction.class);
	private BindService bindService;
	
	//关注公众号后，将用户信息存入数据库
	public void subscribe(String openid) {
		Clerk clerk = bindService.getClerkFromWeChatService(openid);
		bindService.saveOrUpdateClerk(clerk);
	}
	
	//取消关注公众号后，将绑定绑定关系删除，不删除用户信息，以及他的报障信息
	public void unsubscribe(String openid) {
		bindService.unBindClerkAndTerminal(openid);
		
	}
	//验证链接合法性以及是否超时
	public String validateBindLink() {
		HttpServletRequest request = request();
		HttpSession session = session();
		String openid = request.getParameter("openid");
		String terminalNo = request.getParameter("terminalNo");
		String signature = request.getParameter("signature");
		String timestamp = request.getParameter("timestamp");
		
		logger.info("sessionId:" + session.getId());
		logger.info("openid:" + openid);
		logger.info("terminalNo:" + terminalNo);
		logger.info("signature:" + signature);
		logger.info("timestamp:" + timestamp);
		
		//TODO 进行合法性校验
		session.setAttribute("openid", openid);
		session.setAttribute("terminalNo", terminalNo);
		
		return SUCCESS;
	}
	
	//输入手机号，确认绑定
	public String confirmBind() {
		String result;
		HttpSession session = session();
		HttpServletRequest request = request();
		
		String tel = (String) request.getParameter("tel");
		String openid = (String) session.getAttribute("openid");
		String terminalNo = (String) session.getAttribute("terminalNo");
		
		logger.info("sessionId:" + session.getId());
		logger.info("tel:" + tel);
		logger.info("openid:" + openid);
		logger.info("terminalNo:" + terminalNo);
		
		result = bindService.bindClerkAndTerminal(openid,terminalNo,tel);
		if(result.equals(SUCCESS))
			return SUCCESS;
		else if(result.equals("duplicate"))
			return "duplicate";
		else 
		return ERROR;
	}
	
	/**
	 * 获取绑定终端信息
	 * @return
	 */
	public void bindInfo() {
		HttpServletRequest request = request();
		HttpServletResponse response = response();
		String openid = request.getParameter("openid");
		response.setContentType("text/plain;charset=UTF-8");
		PrintWriter out;
		try {
			out = response.getWriter();
			//通过openid获取用户绑定的所有终端set集合
			Set<Terminal> terminals = bindService.getClerkBindTerminals(openid);
			
			JSONArray terminalsArray = new JSONArray();
			//将set集合转换成json数组
			Iterator it = terminals.iterator();
			while(it.hasNext()) {
				Terminal terminal = (Terminal) it.next();
				JSONObject terminalObject = new JSONObject();
				terminalObject.put("terminalNo", terminal.getTerminalNo());
				terminalObject.put("terminalType", terminal.getTerminalType());
				terminalObject.put("address", terminal.getAddress());
				terminalsArray.add(terminalObject);
			}
			
			JSONObject resultObject = new JSONObject();
			resultObject.put("nickname", "nickname");
			resultObject.put("terminals", terminalsArray);
			out.print(resultObject.toString());
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	public BindService getBindService() {
		return bindService;
	}

	public void setBindService(BindService bindService) {
		this.bindService = bindService;
	}
	
}
